package com.ztf.shopping.dao.imp;

import com.ztf.shopping.bean.Article;
import com.ztf.shopping.bean.Order;
import com.ztf.shopping.bean.User;
import com.ztf.shopping.dao.UserDao;
import com.ztf.shopping.untils.JDBCUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImp implements UserDao {

    @Override
    public String findName(String name) {
        Connection connection = null;
        PreparedStatement prep = null;
        ResultSet resultSet = null;
        String sql = "select id from shop_user where name=?";
        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setString(1,name);
            resultSet = prep.executeQuery();
            if (resultSet.next()){
                return resultSet.getString(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,prep,connection);
        }
        return null;
    }

    @Override
    public User login(User user) {
        Connection connection = null;
        PreparedStatement prep = null;
        ResultSet resultSet = null;
        String sql = "select * from shop_user where name=? and password=?";
        User loginUser = null;
        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setString(1,user.getName());
            prep.setString(2,user.getPwd());
            resultSet = prep.executeQuery();
            if (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String password = resultSet.getString(3);
                String money = resultSet.getString(4);
                String head = resultSet.getString(5);
                String email = resultSet.getString(6);
                int isReg = resultSet.getInt(7);
                if (isReg==0){
                    return null;
                }else {
                    loginUser = new User(id,name,password,new BigDecimal(money),head,email);
                    return loginUser;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,prep,connection);
        }
        return null;
    }

    @Override
    public boolean registerServlet(User regUser) {
        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "insert into shop_user(name ,password,email) values (?,?,?)";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setString(1,regUser.getName());
            prep.setString(2,regUser.getPwd());
            prep.setString(3,regUser.getEmail());
            int i = prep.executeUpdate();
            if (i==1){
                return true;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }

        return false;
    }

    @Override
    public List<Order> findShoppingCart(String name) {
        Connection connection = null;
        PreparedStatement prep = null;
        ResultSet resultSet = null;
        String sql = "select * from shopping_car where user_name=? and isPay=0";
        Order order = null;
        List<Order> list = new ArrayList<Order>();
        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setString(1, name);
            resultSet = prep.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt(1);//订单编号
                int articleId = resultSet.getInt(2);//商品id

                String userName = resultSet.getString(3);
                String articleName = resultSet.getString(4);
                int number = resultSet.getInt(5);
                String money = resultSet.getString(6);

                order= new Order(id,articleId,userName,articleName,number,money);
                list.add(order);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,prep,connection);
        }
        return list;
    }

    @Override
    public void sureRegister(int i,String name) {
        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "update shop_user set isRegister=? where name=?";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setInt(1,i);
            prep.setString(2,name);
            int i1 = prep.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }
    }

    @Override
    public Article findArtilceCount(int parseInt) {
        Connection connection = null;
        PreparedStatement prep = null;
        ResultSet resultSet = null;
        String sql = "select * from article where id=?";
        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setInt(1,parseInt);
            resultSet = prep.executeQuery();
            if (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String price = resultSet.getString(3);
                String pic = resultSet.getString(4);
                int number = resultSet.getInt(5);
                return new Article(id,name,new BigDecimal(price),pic,number);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,prep,connection);
        }

        return null;
    }

    @Override
    public void changeCountById(String name, String name1, int i, String s) {
        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "update shopping_car set number=?,money=? where user_name=? and article_name=? and isPay=0";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setInt(1,i);
            prep.setString(2,s);
            prep.setString(3,name);
            prep.setString(4,name1);
            int i1 = prep.executeUpdate();


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }
    }

    @Override
    public void delOrder(String name, String id) {
        //id 是商品id 名字是用户名

        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "delete from shopping_car where user_name=? and articleId=? and isPay=0";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setString(1,name);
            prep.setString(2,id);

            int i1 = prep.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }
    }

    @Override
    public Order findShoppingCartByArticleId(String userName, int articleId) {
        Connection connection = null;
        PreparedStatement prep = null;
        ResultSet resultSet = null;
        String sql = "select * from shopping_car where articleId=? and user_name=? and isPay=0";
        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setInt(1,articleId);
            prep.setString(2,userName);

            resultSet = prep.executeQuery();
            if (resultSet.next()){
                int id = resultSet.getInt(1);
                int articleId2 = resultSet.getInt(2);
                String userName2 = resultSet.getString(3);
                String articleName = resultSet.getString(4);
                int number = resultSet.getInt(5);
                String money = resultSet.getString(6);
                return new Order(id,articleId2,userName2,articleName,number,money);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,prep,connection);
        }
        return null;

    }

    @Override
    public void addOrder(Order order) {
        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "insert into shopping_car(articleId ,user_name,article_name,number,money) values (?,?,?,?,?)";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setInt(1,order.getArticleId());
            prep.setString(2,order.getUserName());
            prep.setString(3,order.getArticleName());
            prep.setInt(4,order.getNumber());
            prep.setString(5,order.getMoney());
            int i = prep.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }
    }

    @Override
    public void payOrder(String name, List<Order> list) {
        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "update shopping_car set isPay=? where user_name=? and articleId=? and isPay=0";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            for (Order order : list) {
                prep.setInt(1,2);
                prep.setString(2,name);
                prep.setInt(3,order.getArticleId());

                //添加批处理
                prep.addBatch();
            }

            //执行批处理命令
            prep.executeBatch();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }
    }

    @Override
    public void changeUser(User loginUser) {
        Connection connection = null;
        PreparedStatement prep = null;

        String sql = "update shop_user set password=?,money=?,head=?,email=? where id=?";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            prep.setString(1,loginUser.getPwd());
            prep.setString(2,loginUser.getMoney().toString());
            prep.setString(3,loginUser.getHead());
            prep.setString(4,loginUser.getEmail());

            prep.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(prep,connection);
        }
    }
}
